{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# 数组和数的计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "import numpy as np"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 4.1数组和数的计算"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]\n",
      " [16 17 18 19]\n",
      " [20 21 22 23]]\n",
      "--------------------\n",
      "[[ 2  3  4  5]\n",
      " [ 6  7  8  9]\n",
      " [10 11 12 13]\n",
      " [14 15 16 17]\n",
      " [18 19 20 21]\n",
      " [22 23 24 25]]\n",
      "--------------------\n",
      "[[ 0  2  4  6]\n",
      " [ 8 10 12 14]\n",
      " [16 18 20 22]\n",
      " [24 26 28 30]\n",
      " [32 34 36 38]\n",
      " [40 42 44 46]]\n",
      "--------------------\n",
      "[[ 0.   0.5  1.   1.5]\n",
      " [ 2.   2.5  3.   3.5]\n",
      " [ 4.   4.5  5.   5.5]\n",
      " [ 6.   6.5  7.   7.5]\n",
      " [ 8.   8.5  9.   9.5]\n",
      " [10.  10.5 11.  11.5]]\n"
     ]
    }
   ],
   "source": [
    "# 由于numpy的广播机制在运算过程中，加减乘除的值被广播到所有的元素上面\n",
    "t1 = np.arange(24).reshape(6,4)\n",
    "print(t1)\n",
    "print(\"-\"*20)\n",
    "t2=t1.tolist()\n",
    "print(t1+2) #ndarray可以与整数进行加减乘除\n",
    "# print(t2+2) 不能对列表进行直接加整数操作\n",
    "print(\"-\"*20)\n",
    "print(t1*2)\n",
    "print(\"-\"*20)\n",
    "print(t1/2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 4.2数组和数组之间的操作"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1]\n",
      " [2 3]\n",
      " [4 5]\n",
      " [6 7]\n",
      " [8 9]]\n",
      "--------------------\n",
      "[[10 11]\n",
      " [12 13]\n",
      " [14 15]\n",
      " [16 17]\n",
      " [18 19]]\n",
      "--------------------\n",
      "[[10 12]\n",
      " [14 16]\n",
      " [18 20]\n",
      " [22 24]\n",
      " [26 28]]\n"
     ]
    }
   ],
   "source": [
    "#同种形状的数组对应位置进行计算\n",
    "t1 = np.arange(10).reshape(5,2)\n",
    "print(t1)\n",
    "print(\"-\"*20)\n",
    "t2 = np.arange(10,20).reshape(5,2)\n",
    "print(t2)\n",
    "print(\"-\"*20)\n",
    "print(t1+t2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2 3 4]\n",
      " [5 6 7 8 9]]\n",
      "--------------------\n",
      "[[10 11]\n",
      " [12 13]\n",
      " [14 15]\n",
      " [16 17]\n",
      " [18 19]]\n",
      "--------------------\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "operands could not be broadcast together with shapes (2,5) (5,2) ",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mValueError\u001B[0m                                Traceback (most recent call last)",
      "Input \u001B[1;32mIn [37]\u001B[0m, in \u001B[0;36m<cell line: 8>\u001B[1;34m()\u001B[0m\n\u001B[0;32m      6\u001B[0m \u001B[38;5;28mprint\u001B[39m(t2)\n\u001B[0;32m      7\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m-\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;241m*\u001B[39m\u001B[38;5;241m20\u001B[39m)\n\u001B[1;32m----> 8\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[43mt1\u001B[49m\u001B[38;5;241;43m+\u001B[39;49m\u001B[43mt2\u001B[49m)\n",
      "\u001B[1;31mValueError\u001B[0m: operands could not be broadcast together with shapes (2,5) (5,2) "
     ]
    }
   ],
   "source": [
    "# 不同形状的多维数组不能进行计算\n",
    "t1 = np.arange(10).reshape(2,5)\n",
    "print(t1)\n",
    "print(\"-\"*20)\n",
    "t2 = np.arange(10,20).reshape(5,2)\n",
    "print(t2)\n",
    "print(\"-\"*20)\n",
    "print(t1+t2)\n",
    "\n",
    "# operands could not be broadcast together with shapes (2,5) (5,2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "#一维数组和二维数组进行运算时，一维的元素个数和列数相等\n",
    "t1 = np.arange(10).reshape(2,5)\n",
    "t2 = np.arange(5)\n",
    "print(t1)\n",
    "print(t2)\n",
    "print(t1-t2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "t1 = np.arange(24).reshape((4,6))\n",
    "t2 = np.arange(4).reshape((4,1))\n",
    "print(t2)\n",
    "print(t1)\n",
    "print(t1-t2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 4.3 数组中的轴"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "t1=np.arange(1,19).reshape(3,2,3)\n",
    "print(t1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "--------------------\n",
      "[5 7 9]\n",
      "--------------------\n",
      "[ 6 15]\n",
      "--------------------\n",
      "21\n",
      "[[[ 0  1  2]\n",
      "  [ 3  4  5]\n",
      "  [ 6  7  8]]\n",
      "\n",
      " [[ 9 10 11]\n",
      "  [12 13 14]\n",
      "  [15 16 17]]\n",
      "\n",
      " [[18 19 20]\n",
      "  [21 22 23]\n",
      "  [24 25 26]]]\n",
      "--------------------\n",
      "[[27 30 33]\n",
      " [36 39 42]\n",
      " [45 48 51]]\n",
      "--------------------\n",
      "[[ 9 12 15]\n",
      " [36 39 42]\n",
      " [63 66 69]]\n",
      "--------------------\n",
      "[[ 3 12 21]\n",
      " [30 39 48]\n",
      " [57 66 75]]\n",
      "[[[[  0   1   2   3   4]\n",
      "   [  5   6   7   8   9]\n",
      "   [ 10  11  12  13  14]\n",
      "   [ 15  16  17  18  19]]\n",
      "\n",
      "  [[ 20  21  22  23  24]\n",
      "   [ 25  26  27  28  29]\n",
      "   [ 30  31  32  33  34]\n",
      "   [ 35  36  37  38  39]]\n",
      "\n",
      "  [[ 40  41  42  43  44]\n",
      "   [ 45  46  47  48  49]\n",
      "   [ 50  51  52  53  54]\n",
      "   [ 55  56  57  58  59]]]\n",
      "\n",
      "\n",
      " [[[ 60  61  62  63  64]\n",
      "   [ 65  66  67  68  69]\n",
      "   [ 70  71  72  73  74]\n",
      "   [ 75  76  77  78  79]]\n",
      "\n",
      "  [[ 80  81  82  83  84]\n",
      "   [ 85  86  87  88  89]\n",
      "   [ 90  91  92  93  94]\n",
      "   [ 95  96  97  98  99]]\n",
      "\n",
      "  [[100 101 102 103 104]\n",
      "   [105 106 107 108 109]\n",
      "   [110 111 112 113 114]\n",
      "   [115 116 117 118 119]]]]\n",
      "--------------------------------------------------\n",
      "[[[ 60  63  66  69  72]\n",
      "  [ 75  78  81  84  87]\n",
      "  [ 90  93  96  99 102]\n",
      "  [105 108 111 114 117]]\n",
      "\n",
      " [[240 243 246 249 252]\n",
      "  [255 258 261 264 267]\n",
      "  [270 273 276 279 282]\n",
      "  [285 288 291 294 297]]]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[1,2,3],[4,5,6]])\n",
    "print(a)\n",
    "print(\"-\"*20)\n",
    "print(np.sum(a,axis=0)) # [5 7 9]\n",
    "print(\"-\"*20)\n",
    "print(np.sum(a,axis=1)) # [ 6 15]\n",
    "print(\"-\"*20)\n",
    "print(np.sum(a))\n",
    "a=np.arange(27).reshape((3,3,3))\n",
    "print(a)\n",
    "print(\"-\"*20)\n",
    "b=np.sum(a, axis=0)\n",
    "print(b)\n",
    "print(\"-\"*20)\n",
    "c=np.sum(a, axis=1)\n",
    "print(c)\n",
    "d=np.sum(a, axis=2)\n",
    "print(\"-\"*20)\n",
    "print(d)\n",
    "\n",
    "b=np.arange(120).reshape((2,3,4,5))\n",
    "print(b)\n",
    "c=np.sum(b,axis=1)\n",
    "print('-'*50)\n",
    "print(c)\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}